package com.dao.imp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
	public static synchronized Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/schooltest?useUnicode=true&characterEncoding=utf-8", "root",
					"249658364");
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}

	public static void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	public static void close(Statement pstmt) {
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}
	}

	public static boolean add(final String sql){
				Connection conn = DbUtil.getConnection();
				synchronized(conn)
				{
					Statement stat = null;
					try {
						stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
								ResultSet.CONCUR_UPDATABLE);
			stat.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			DbUtil.close(stat);
			DbUtil.close(conn);
			return false;
		}
		DbUtil.close(stat);
		DbUtil.close(conn);
		return true;
		}
	}

	public static boolean delet(String tablename, String idname, int id) {
		Connection conn = DbUtil.getConnection();
		Statement stat = null;
		try {
			stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stat.executeQuery("select * from " + tablename
					+ " where " + idname + "=" + id + ";");
			if (rs.next()) {
				rs.deleteRow();
				DbUtil.close(rs);
				DbUtil.close(stat);
				DbUtil.close(conn);
				return true;
			} else {
				DbUtil.close(rs);
				DbUtil.close(stat);
				DbUtil.close(conn);
				return false;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			DbUtil.close(stat);
			DbUtil.close(conn);
			return false;
		}
	}

	public static boolean change(String tablename, String idname, int id,
			String str, Object temp) {
		Connection conn = DbUtil.getConnection();
		Statement stat = null;
		try {
			stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stat.executeQuery("select * from " + tablename
					+ " where " + idname + "=" + id + ";");
			if (rs.next()) {
				rs.updateObject(str, temp);
				rs.updateRow();
				DbUtil.close(rs);
				DbUtil.close(stat);
				DbUtil.close(conn);
				return true;
			} else {
				DbUtil.close(rs);
				DbUtil.close(stat);
				DbUtil.close(conn);
				return false;
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			DbUtil.close(stat);
			DbUtil.close(conn);
			return false;
		}
	}

	public static ResultSet Query(String str) {
		Connection conn = DbUtil.getConnection();
		Statement stat = null;
		try {
			stat = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = stat.executeQuery(str);
			return rs;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return null;
		}

	}
}